package com.wasion.pwportable.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import com.wasion.pwportable.domain.po.Point;

import android.util.Xml;

public class PullPointsService {

	/**
	 * ------------------------使用PULL解析XML-----------------------
	 * 
	 * @param inStream
	 * @return
	 * @throws Exception
	 */
	public static List<Point> getPoints(InputStream inStream) throws Exception {
		Point p = null;
		List<Point> points = null;
		XmlPullParser pullParser = Xml.newPullParser();
		pullParser.setInput(inStream, "UTF-8");
		int event = pullParser.getEventType();// 觸發第一個事件
		while (event != XmlPullParser.END_DOCUMENT) {
			switch (event) {
			case XmlPullParser.START_DOCUMENT:
				points = new ArrayList<Point>();
				break;
			case XmlPullParser.START_TAG:
				if ("Point".equals(pullParser.getName())) {
					int addr = Integer.parseInt(pullParser.getAttributeValue(0)
							.substring(2), 16);
					String unit = pullParser.getAttributeValue(2);
					String name = pullParser.getAttributeValue(3);

					p = new Point();
					p.setPoint(addr);
					p.setUnit(unit);
					p.setName(name);
				}
				break;
			case XmlPullParser.END_TAG:
				if ("Point".equals(pullParser.getName())) {
					points.add(p);
					p = null;
				}
				break;
			}
			event = pullParser.next();
		}
		return points;
	}
}
